<script type="text/javascript" src="vue.min.js"></script>
<div id="app">
</div>
<script type="module">
  import webSocketFactory from "/WebSocket.js"
  import {getWebSocketUrl} from '/PublicConfig.js'
  new Vue({
      el: '#app',
      data: function () {
        return {
            serverHost: getWebSocketUrl(),
            userInfo: {"userId": "111111", "name": "longxiaonan", "tenantId": "ttttt1111"}
        }
      },
      created() {
          // 开启websocket
          this.opentSocketConnect()
      },
      destroyed: function () {
        this.websocketclose();
      },
      methods: {
        opentSocketConnect: function () {
           this.socketConnect()
        },
        socketConnect () {
          if (this.userInfo.userId !== undefined) {
              let connectPath = this.serverHost + this.userInfo.userId + '/' + this.userInfo.tenantId + '/' + this.userInfo.name
              console.log('connectPath', connectPath)
              this.socket = webSocketFactory.connect(connectPath)
              let that = this
              this.socket.onmessage(event => {
                  // webSocketFactory.resetHeartCheck(this.userInfo.userId, this.socket)
                  let respData = JSON.parse(event.data)
                  console.log('接收到待处理数的推送', respData)
                  if (respData.method === 'FLOW_NOTIFY') {
                  let bzCode = respData.bzCode
                  let msg = '<h4>类型：' + respData.type + '</h4>' +
                      '<h6>业务单号：' + bzCode + '</h6>' +
                      '<h6>内容：' + respData.content + '</h6>'
                  this.showNotify({title: respData.title, message: msg})
              } else if (respData.method === 'GET_TODO_NUM') { // 接收到待处理数的推送
                  // this.todo.count = respData.data
              } else if (respData.method === 'LINK_OPEN') {
                  // this.topData.onlineUsers = respData.data
              }
          })
              this.socket.onopen(event => {
                  webSocketFactory.startHeartCheck(that.userInfo.userId, that.socket)
                  // that.getOnlineUser()
              })
              this.socket.onclose(event => {
                  console.log('WebSocket连接关闭, 发起重连')
                  that.socketReConnect()
              })
              this.socket.onerror(event => {
                  console.log('WebSocket连接异常, 发起重连')
                  that.socketReConnect()
              })
          }
        },
        socketReConnect () {
          if (this.reConnectFlag) {
              return
          }
          let that = this
          this.reConnectFlag = true
          setTimeout(function () {
              console.log('开始重连' + new Date())
              that.socketConnect()
              that.reConnectFlag = false
          }, 6000)
        },
        socketConnectClose () {
          this.socket.close()
        },
        showNotify ({title, message}) {
          this.$notify({
              title: title,
              dangerouslyUseHTMLString: true,
              message: message
          })
        }
      }
    }

  );
</script>
